<?php
	class OrdenesController extends AppController {

		var $name = 'Ordenes';
		var $helpers = array(
				'Paginator',
				'Form',
				'Time',
				'Ajax',
				'Foto'
		);
		var $paginate = array('Orden' => array(
					'limit' => 25,
					'recursive' => 3,
				//'order' => array('Articulo.detalle' => 'asc', )
			), );

		function index() {
			$this -> Orden -> recursive = 0;
			$this -> set('ordenes', $this -> paginate());
		}

		function view($id = null) {
			if (!$id) {
				$this -> Session -> setFlash(__('Invalid orden', true));
				$this -> redirect(array('action' => 'index'));
			}
			$this -> set('orden', $this -> Orden -> read(null, $id));
		}

		function add() {
			if (!empty($this -> data)) {
				$this -> Orden -> create();
				if ($this -> Orden -> save($this -> data)) {
					$this -> Session -> setFlash(__('The orden has been saved', true));
					$this -> redirect(array('action' => 'index'));
				} else {
					$this -> Session -> setFlash(__('The orden could not be saved. Please, try again.', true));
				}
			}
			$clientes = $this -> Orden -> Pedido -> Cliente -> find('list');
			$articulos = $this -> Orden -> Articulo -> find('list');
			$pedidos = $this -> Orden -> Pedido -> find('list');
			$this -> set(compact('clientes', 'articulos', 'pedidos'));
		}

		function edit($id = null) {
			if (!$id && empty($this -> data)) {
				$this -> Session -> setFlash(__('Invalid orden', true));
				$this -> redirect(array('action' => 'index'));
			}
			if (!empty($this -> data)) {
				if ($this -> Orden -> save($this -> data)) {
					$this -> Session -> setFlash(__('The orden has been saved', true));
					$this -> redirect(array('action' => 'index'));
				} else {
					$this -> Session -> setFlash(__('The orden could not be saved. Please, try again.', true));
				}
			}
			if (empty($this -> data)) {
				$this -> data = $this -> Orden -> read(null, $id);
			}
			$articulos = $this -> Orden -> Articulo -> find('list');
			$pedidos = $this -> Orden -> Pedido -> find('list');
			$this -> set(compact('articulos', 'pedidos'));
		}

		function delete($id = null) {
			if (!$id) {
				$this -> Session -> setFlash(__('Invalid id for orden', true));
				$this -> redirect(array('action' => 'index'));
			}
			if ($this -> Orden -> delete($id)) {
				$this -> Session -> setFlash(__('Orden deleted', true));
				$this -> redirect(array('action' => 'index'));
			}
			$this -> Session -> setFlash(__('Orden was not deleted', true));
			$this -> redirect(array('action' => 'index'));
		}

		function preparar($pedido_id = null) {
			if (isset($this -> params['form']['finalizar'])) {
				# si se presionó el botón Finalizar Pedido...
				if (!$pedido_id && empty($this -> data)) {
					$this -> Session -> setFlash('Pedido Inválido');
					$this -> redirect(array(
							'controller' => 'pedidos',
							'action' => 'index'
					));
				}
				if (!empty($this -> data)) {
					$this -> Orden -> Pedido -> save($this -> data['Pedido']);
					foreach ($this->data['Orden'] as $index => $orden) {
						if ($this -> Orden -> save($orden)) {
							$this -> Session -> setFlash('El Pedido ha sido Finalizado');
						} else {
							$this -> Session -> setFlash('Se ha producido un error. Por favor, avise al Administrador.');
						}
					}
					$this -> redirect(array(
							'controller' => 'pedidos',
							'action' => 'index'
					));
				}
			} elseif (isset($this -> params['form']['cancelar'])) {
				# si se presionó el botón Cancelar...
				$this -> redirect(array(
						'controller' => 'pedidos',
						'action' => 'index'
				));
			}
			$this -> set('pedido', $this -> Orden -> Pedido -> read(null, $pedido_id));
			$this -> set('articulos', $this -> paginate('Orden', array('Orden.pedido_id' => $pedido_id)));
		}

		function agregar($articulo_id) {
			$this -> layout = 'ajax';
			if (!empty($articulo_id)) {
				debug($articulo_id);
				$articulo = $this -> Orden -> Articulo -> findById($articulo_id);
				$this -> set(compact('articulo'));
				$this -> render("/elements/ordenes_pedido");
			}
		}

	}
?>